$(function () {
//验证规则
    $("#name,#tel").textbox({
            onChange: function (newValue, oldValue) {
                var name = $("#client_form [name=name]").val();
                var tel = $("#client_form [name=tel]").val();
                console.log(tel);
                if (name && tel) {
                    $("#name").textbox({
                        validType: "remote['/client/validateName.do?tel='+tel.value,'name']"
                    });
                } else {
                    return;
                }
            }
        }
    )
    var client_datagrid = $("#client_datagrid");
    var client_dialog = $("#client_dialog");
    var client_form = $("#client_form");
    var exam_dialog = $("#exam_dialog");
    var query_dialog = $("#query_dialog");
    var query_form = $("#query_form");
    var formal_dialog = $("#formal_dialog");
    var formal_form = $("#formal_form");
    var tracking_form = $("#tracking_form");
    var tracking_dialog = $("#tracking_dialog");
    client_datagrid.datagrid({
        url: '/client/query.do',
        fit: true,
        // fitColumns: true,
        rownumbers: true,
        columns: [[
            {field: 'name', title: '姓名', width: 100,},
            {
                field: 'salemen', title: '营销人员', width: 100, formatter: function (value) {
                return value.username;
            }
            },
            {field: 'traceNumber', title: '跟踪次数', width: 100,},
            {field: 'lastTime', title: '最后跟踪时间', width: 100,},
            {field: 'postionTime', title: '约访时间', width: 100,},
            {field: 'nextTime', title: '下次跟进时间', width: 100,},
            {field: 'qq', title: 'QQ', width: 100,},
            {field: 'tel', title: '电话', width: 100,},
            {field: 'schoolName', title: '学校', width: 100,},
            {
                field: 'intention', title: '意向程度', width: 100, formatter: function (value) {
                if (value) {
                    return value.name;
                }
            }
            },
            {
                field: 'intentionArea', title: '意向校区', width: 100, formatter: function (value) {
                if (value) {
                    return value.name;
                }
            }
            },
            {
                field: 'intentionClass', title: '意向班级', width: 100, formatter: function (value) {
                if (value) {
                    return value.className;
                }
            }
            },
            {
                field: 'state', title: '状态', width: 100, formatter: function (value) {
                return value ? '<font color="green">正式学员</font>' : '正常';
            }
            },
            {
                field: 'traced', title: '是否跟踪', width: 100, formatter: function (value) {
                return value ? '是' : '否';
            }
            },
            {field: 'remark', title: '备注', width: 100},
        ]],
        singleSelect: true,
        pagination: true,
        toolbar: "#client_btns",
        sortName: 'postion_time',
        sortOrder: 'desc'
    });
    client_dialog.dialog({
        fit: true,
        buttons: "#save_btns",
        closed: true,
        onClose: function () {
            client_form.form('clear');
        }
    });
    formal_dialog.dialog({
        title: '转正',
        buttons: "#formal_btns",
        width: 800,
        height: 500,
        closed: true,
        onClose: function () {
            formal_form.form('clear');
        }

    });
    exam_dialog.dialog({
        title: '考试登记',
        width: 350,
        height: 400,
        buttons: "#exam_btns",
        closed: true,
        onClose: function () {
            $("#exam_form").form('clear');
        }
    });
    query_dialog.dialog({
        title: '高级查询',
        width: 1000,
        height: 300,
        buttons: "#query_btns",
        closed: true,
        onClose: function () {
            $("#query_form").form('clear');
        }
    });
    /*跟踪按钮*/
    tracking_dialog.dialog({
        width: 1000,
        height: 350,
        buttons: "#tracking_btns",
        closed: true,
        onClose: function () {
            $("#tracking_form").form('clear');
        }
    });
    var methodObj = {
        save_formal: function () {
            var row = client_datagrid.datagrid("getSelected");
            formal_form.form('submit', {
                url: '/client/positive.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '操作成功', 'info')
                        methodObj.cancel_formal();
                        client_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'warning');
                    }
                }
            })
        },
        query: function () {
            client_datagrid.datagrid('reload', []);
            query_dialog.dialog('open');
        },
        cancel_query: function () {
            query_dialog.dialog('close');
        },
        cancel_formal: function () {
            formal_dialog.dialog('close');
        },
        cancel_tracking: function () {
            tracking_dialog.dialog('close');
        },
        save_query: function () {
            var name = $("#query_form [name=name]").val();
            var minAge = $("#query_form [name=minAge]").val();
            var maxAge = $("#query_form [name=maxAge]").val();
            var gender = $("#query_form [name=gender]").val();
            var education = $("#query_form [name=education]").val();
            var intention = $("#query_form [name=intention]").val();
            var state = $("#query_form [name=state]").val();
            var intentionClass = $("#query_form [name=intentionClass]").val();
            var intentionArea = $("#query_form [name=intentionArea]").val();
            var intentionMajor = $("#query_form [name=intentionMajor]").val();
            client_datagrid.datagrid('load', {
                name: name,
                minAge: minAge,
                maxAge: maxAge,
                gender: gender,
                education: education,
                intention: intention,
                state: state,
                intentionClass: intentionClass,
                intentionArea: intentionArea,
                intentionMajor: intentionMajor
            });
            methodObj.cancel_query();
        },
        formal: function () {
            client_datagrid.datagrid('load', {
                state: true
            })
        },
        positive: function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            var intentionClass = row.intentionClass;
            var intentionArea = row.intentionArea
            row['clientId'] = row.id;
            row['salemen.id'] = row.salemen.id;
            row['intentionArea'] = intentionArea.id;
            row['intentionClass.id'] = intentionClass.id;
            row['customerType.id'] = row.customerType;
            formal_form.form('load', row);
            row['intentionClass'] = intentionClass;
            row['intentionArea'] = intentionArea;
            if (row.state) {
                $.messager.alert('温馨提示', "已经是正式学员,无法转正", 'warning');
                return;
            }
            formal_dialog.dialog('open');

            $(".money1").textbox({
                onChange: function (value) {
                    var programTuition = $("#programTuition").val() || 0;
                    var otherExpense = $("#otherExpense").val() || 0;
                    var discountAmount = $("#discountAmount").val() || 0;
                    var otherDiscount = $("#otherDiscount").val() || 0;
                    var planTuition = $("#planTuition").val() || 0;
                    var prepaidTuition = $("#prepaidTuition").val() || 0;

                    var sum = parseFloat(programTuition) + parseFloat(otherExpense);
                    var subtract = parseFloat(discountAmount) + parseFloat(otherDiscount) + parseFloat(planTuition) + parseFloat(prepaidTuition);
                    var total = parseFloat(sum) - parseFloat(subtract);

                    var totalTuition = $("#totalTuition").textbox("setValue", parseFloat(sum));
                    $("#debt").textbox("setValue", parseFloat(total));

                }
            });
        },
        //放入资源池
        resourcePool: function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定要将该客户放入资源池吗？', function (r) {
                if (r) {
                    $.get("/client/resourcePool.do", {clientId: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info')
                            client_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        //考试登记按钮
        save_exam: function () {
            $("#exam_form").form('submit', {
                url: '/exam/save.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '登记成功', 'info')
                        methodObj.cancel_exam();
                        client_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            });
        },
        cancel_exam: function () {
            exam_dialog.dialog('close');
        },
        //考试登记页面弹框
        exam: function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            row['salerName'] = row.salemen.username;
            row['wantClass'] = row.intentionClass.className;
            $("#exam_form").form('load', row);
            exam_dialog.dialog('open');
        },
        add: function () {
            client_dialog.dialog('setTitle', '新增潜在客户');
            client_dialog.dialog('open');
        },

        edit: function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            row['intentionClass.id'] = row.intentionClass.id;
            row['intentionArea.id'] = row.intentionArea.id;
            row['intention.id'] = row.intention.id;
            row['salemen.id'] = row.salemen.id;
            client_form.form('load', row);
            client_dialog.dialog('setTitle', '编辑潜在客户');
            client_dialog.dialog('open');
        },
        changeState: function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定执行该操作吗？', function (r) {
                if (r) {
                    $.get("/client/changeState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info')
                            client_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        /*跟踪按钮*/
        tracking: function () {
            var row = client_datagrid.datagrid("getSelected");
            console.log(row);
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            row['id'] = row.id;
            row['salemen.id'] = row.salemen.id;
            row['intentionClass.id'] = row.intentionClass.id;
            row['intentionArea.id'] = row.intentionArea.id;
            row['intention.id'] = row.intention.id;
            tracking_form.form('load', row);
            tracking_dialog.dialog('setTitle', '跟踪潜在客户');
            tracking_dialog.dialog('open');
        },
        /*跟踪保存按钮*/
        save_tracking: function () {
            tracking_form.form('submit', {
                url: '/studentTracking/trackingSave.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '跟踪成功!', 'info')
                        tracking_dialog.dialog('close');
                        client_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },
        del: function () {
            var row = client_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定要删除该潜在客户吗？', function (r) {
                if (r) {
                    $.get("/client/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '删除成功', 'info')
                            client_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            client_datagrid.datagrid('load', []);
        },

        save: function () {
            client_form.form('submit', {
                url: '/client/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info')
                        methodObj.cancel();
                        client_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },

        cancel: function () {
            client_dialog.dialog('close');
        }
    }
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data('cmd');
        console.log(methodName)
        methodObj[methodName]();
    })


})



